<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ include file="/website/common/base.jsp"%>

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>${webname }-采购模板</title>
<%@ include file="/website/common/common.jsp"%>
<link href="${webctx }/css/manage.css" rel="stylesheet" type="text/css" />
<link href="${webctx }/css/payment.css" rel="stylesheet" type="text/css" />
<style>

	.buy-btn a{ width:100px; height:22px; border:1px solid #ff6767; color:#ff6767; display:block; font-size:12px; padding-left:2px; text-indent:20px;line-height:22px;}
	.buy-btn i{ width:15px; height:16px; position:absolute; left:8px; top:4px; background:url(${webctx}/images/buyico.png) no-repeat;}
	.buy-btn a:hover{ color:#999; border:1px solid #999}
	.buy-btn3{margin-top:5px;}
	.buy-btn3 a{font-size:14px;}
	.acc-btn{ width:120px; height:30px; line-height:30px;display:inline-block; font-size:15px; border-radius:2px; text-align:center;background:#fff; color:#23b0fc; border:1px solid #23b0fc}
	.acc-btn:hover{background:#14ABFC;color:#fff}

	.th-info{width:400px;}
	.th-price{ width:150px;}
	.th-amount{ width:150px;}
	.td-pic{ width:105px;}
	.td-price{ width:150px;}
	.td-amount{ width:120px;}
	.td-amount .buy-Num-bd{ margin-top:20px; width:50px; height:28px; line-height:28px; padding:0 18px;}
	.td-amount .buy-Num-bd span{ width:24px; line-height:28px; height:28px;}
	.td-amount .buy-Num-bd input{ width:30px; height:22px; line-height:22px;padding-left:8px}
	.td-amount em{ padding:30px 0 0 45px; display:inline-block}
	.remove a{border:1px solid #999;color: #999;}

</style>
</head>

<body>
<%@ include file="/website/common/top.jsp"%>
<%@ include file="/website/common/head.jsp"%>
<%@ include file="/website/common/menu.jsp"%>

<div class="warp-body">
	<div class="inner clearfix">
    	<div class="left-main">
        	<div class="left-inner">
            	<%@ include file="/website/user/left.jsp"%>
            </div>
        </div>
    	<div class="right-main">
        	<div class="right-inner" style="background:#fff">
            	<div class="right-title"><span></span>添加商品</div>
            	<div class="line8"></div><div class="line8"></div>
            	<input name="panid" type="hidden" value="${bean.id }">
            	<div>
            		<span style="float:left;margin-left:10px">
            			<p style="padding:5px;"><font style="font-size:16px;">${bean.name }</font><font style="color:#747474;padding-left:20px;">${bean.explanation }</font></p>
            			<p><input name="like" value="${like }" style="width:300px;height:28px;border:1px solid #ececec;margin-left:5px;" class="marginR8" placeholder="请输入商品名称、生产厂家"><a class="acc-btn" href="javascript:query();" style="width:90px;height:31px;">搜索</a></p>
            		</span>
            		<span style="float:right;padding-top:14px;"><a class="acc-btn marginR8" href="javascript:addseled(${bean.id });">添加选中商品</a></span>
            	</div>
            	<div class="line8 clear"></div><div class="line8"></div>
            	<!-- start -->
            	<div class="cart-th">
	            	<ul class="clearfix">
	            		<li class="th th-chk">
	                    	<div class="cart-checkbox allbox">
	                            <input name="cartcheckbox" type="checkbox" value="" />
	                            <label class="J_select">
	                            	<span class="chk-ico"></span> 全选
	                            </label>
	                        </div>
	                    </li>
	            		<li class="th th-info"><div class="th-inner">商品信息</div></li>
	            		<li class="th th-price"><div class="th-inner">单价（元）</div></li>
	            		<li class="th th-amount"><div class="th-inner">数量</div></li>
	            		<li class="th th-op"><div class="th-inner">操作</div></li>
	            	</ul>
	            </div>
            	<div class="cart-bd">
            	<c:forEach items="${list }" var="product" varStatus="status">
	            	<div class="order-content" pid="${product.id }">
	                	<ul class="item-content clearfix">
	                		<li class="td td-chk">
	                			<c:if test="${product.amount>0 }">
	                        	<div class="cart-checkbox productbox">
	                            	<input name="cartcheckbox" type="checkbox" value="${product.id}"/>
	                            	<label class="J_select">
	                            	</label>
	                            </div>
	                            </c:if>
	                        </li>
	                        <li class="td td-pic">
	                        	<div class="td-inner">
	                            	<a href="${ctx }/product/info?priceid=${product.id}" target="_blank"><img src="<%=getRandomFileserver()%>${product.photo}" width="90" height="90" alt="" onerror="this.src='${webctx }/images/noimg.jpg'"></a>
	                            </div>
	                        </li>
	                		<li class="td td-info">
	                        	<div class="td-inner">
	                            	<div class="info-title">${product.productname } ${product.name }</div>
	                                <ul class="info-txt">
	                                	<li class="clearfix">
	                                    	<span class="tl">生产厂家： ${product.B_chanjia }</span>
	                                        <span>件装量： ${product.B_zhuanl }</span>
	                                    </li>
	                                    <li class="clearfix">
	                                    	<span class="tl">规　　格： ${product.B_guige }</span>
	                                        <span>单　位： ${product.unitname }</span>
	                                    </li>
	                                    <li class="clearfix">
	                                    	<span class="tl">批准文号：${product.B_pzwh }</span>
	                                        <span>可拆零： ${product.B_kechai }</span>
	                                    </li>
	                                </ul>
	                            </div>
	                        </li>
	                		<li class="td td-price">
	                        	<div class="td-inner">
									<c:choose>
										<c:when test="${islogin && isuserauth }">
											<span style="display: block;">单价：￥${product.realprice }</span>
										</c:when>
										<c:when test="${!islogin }">
											<span style="display: block;">单价：登录可见</span>
										</c:when>
										<c:when test="${!isuserauth }">
											<span style="display: block;">单价：审核通过可见</span>
										</c:when>
									</c:choose>
	                                <span style="display: block;">库存：<c:choose><c:when test="${product.amount>100}">>100</c:when><c:when test="${product.amount<=0}">无</c:when><c:otherwise>${product.amount }</c:otherwise></c:choose></span>
	                            </div>
	                        </li>
	                        <li class="td td-amount">
	                        	<div class="td-inner">
	                                <div class="buy-Num-bd across">
	                                    <input name="buynumInp" type="text" class="buy-numInp"onchange="inputAmount(this)" value="1" maxnum="${product.amount }"/>
	                                    <span class="btn-reduce" onclick="lessenamount(${product.id },this)">-</span>
	                                    <span class="btn-add" onclick="addamount(${product.id },this)">+</span>
	                                </div>
	                            </div>
	                        </li>
	                        <li class="td td-op">
	                        	<div class="td-inner">
	                            	<div>
										<c:choose>
											<c:when test="${product.amount>0}">
												<c:choose>
													<c:when test="${product.realprice>0}">
														<c:choose>
															<c:when test="${product.isOwned}"><p class="buy-btn remove"><a><i></i><span class="_template">移出采购模板</span></a></p></c:when>
															<c:otherwise><p class="buy-btn"><a><i></i><span class="_template">加采购模板</span></a></p></c:otherwise>
														</c:choose>
													</c:when>
													<c:otherwise><p class="buy-btn remove"><a href="javascript:void(0);"><i></i><span>加采购模板</span></a></p></c:otherwise>
												</c:choose>
											</c:when>
											<c:otherwise><p class="buy-btn3"><a href="javascript:;">库存不足</a></p></c:otherwise>
										</c:choose>
	                                </div>

	                            </div>
	                        </li>
	                	</ul>
	                </div>

	                </c:forEach>

	                ${pagehtml }
	            </div>
            	<!-- end -->
            </div>
        </div>
    </div>
</div>

<%@ include file="/website/common/foot.jsp"%>
</body>
</html>
<script>
var uploader;
$(function(){
	setLeftCur("cgmb");

	$('.J_select').each(function(){
		if($(this).prev().get(0).checked)
			$(this).parent().addClass('checked');
	})

	$('.J_select').click(function(){
		var ischeck=false;
		if($(this).parent().get(0).className.indexOf("checked")==-1)ischeck=true;
		checkbox($(this).parent(),ischeck);
		if($(this).parent().get(0).className.indexOf("allbox")!=-1)selall(ischeck);
		if(!ischeck){
			$(".allbox").each(function(){
				checkbox($(this),ischeck);
			})
		}
	})
})
function query(){
	gopage(1);
}
function gopage(n){
	window.location.href=ctx+"/purchasetemp/addProductShow?panid="+$("[name='panid']").val()+"&pno="+n+"&like="+$("[name='like']").val();
}
function topageclick(){
	if($("[name='topagenum']").val()!="")
		gopage($("[name='topagenum']").val());
}
$(".buy-btn").click(function () {
    var productarr = [];
    var product = {};
    product["priceid"]=$(this).parents(".order-content").attr("pid");
    product["num"]=$(this).parents(".order-content").find(".buy-numInp").val();
    productarr.push(product);
    if($(this).hasClass('remove')){
        $w.httpRequest({
            url:"${ctx}/purchasetemp/delproduct",
            data:{"panid":"${bean.id}","priceid":product["priceid"]},
            ok:function () {
                alert("移出采购模板成功");
                $("[pid='"+product["priceid"]+"']").find("._template").text("加采购模板");
                $("[pid='"+product["priceid"]+"']").find(".buy-btn").removeClass("remove");
            },
            fail:function () {
                alert("移出采购模板失败");
            }
        })
	}else{
        $w.httpRequest({
            url:"${ctx}/purchasetemp/addproduct",
            data:{"panid":"${bean.id}","data":JSON.stringify(productarr)},
            ok:function () {
                alert("加入采购模版成功");
                $("[pid='"+product["priceid"]+"']").find("._template").text("移出采购模板");
                $("[pid='"+product["priceid"]+"']").find(".buy-btn").addClass("remove");
            },
            fail:function () {
                alert("加入采购模版失败");
            }
        })
	}
})
function addamount(pid,obj){
	 var txt=$("[name='buynumInp']",$(obj).parent());
	  var n=parseInt(txt.val());
	  var maxnum=0;
	  n++;
	  if(txt.attr("maxnum"))maxnum=parseInt(txt.attr("maxnum"));
	  if(n>maxnum)n=maxnum;
	  txt.val(n);
    inputAmount(obj);
}
function lessenamount(pid,obj){
	 var txt=$("[name='buynumInp']",$(obj).parent());
	  var n=parseInt(txt.val());
	  n--;
	  if(n<=0)n=1;
	  txt.val(n);
	  inputAmount(obj);
}
function selall(ischeck){
	$(".productbox").each(function(){
		checkbox($(this),ischeck);
	})
}
function checkbox(obj,ischeck){
	if(ischeck){
		if(obj.get(0).className.indexOf("checked")==-1){
			obj.addClass("checked");
		}
	}else obj.removeClass("checked");
	$("[name='cartcheckbox']",obj).get(0).checked=ischeck;

}
var submiting=false;
function addseled(){
	var seldatas=[];
	$(".productbox").each(function(){
		var box=$("[name='cartcheckbox']",$(this)).get(0);
		var baseobj=$("[pid='"+box.value+"']");
		if(box.checked){
			seldatas.push({"priceid":box.value,"num":$("[name='buynumInp']",baseobj).val()});
		}
	})
	if(seldatas.length==0){
		alert("请选择商品");
		return;
	}
	if($w.checkLogin(true)){
		if(submiting)return;
		submiting=true;
		$w.httpRequest({
			url:apiroot+"/purchasetemp/addproduct",
			data:{"data":JSON.stringify(seldatas),panid:$("[name='panid']").val()},
			ok:function(ret){
				window.location.href=ctx+"/purchasetemp/show?id="+$("[name='panid']").val();
			},fail:function(ret){if(ret.MsgCode=='1001')window.location.href=ctx+"/user/certification";return false},all:function(){submiting=false;}
		})
	}
}
</script>